package com.lqk.fbridge

import android.content.Context
import android.os.Bundle
import android.util.Log
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.FlutterEngineGroup
import io.flutter.plugin.common.MethodChannel

class FlutterBridgeActivity : FlutterActivity() {

    companion object {
        const val CHANNEL_NAME = "flutter_bridge"
        const val TAG = "FlutterBridgeActivity"

        // 参数传递
        const val EXTRA_DART_FILE = "EXTRA_DART_FILE"
        const val EXTRA_FUN_NAME = "EXTRA_FUN_NAME"
        const val EXTRA_ROOT_ROUTE = "EXTRA_ROOT_ROUTE"
        const val EXTRA_PAGE_ROUTE = "EXTRA_PAGE_ROUTE"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        flutterEngine.systemChannel
        var listenerFlutterChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL_NAME)
        listenerFlutterChannel.setMethodCallHandler { call, result ->
            when (call.method) {
                "Native" -> {
                    result.success("Android-Native, 继续累加 +1 ")
                }

                "Text" -> {
                    result.success("Android-Text, 继续累加 +1 ")
                }

                "Channel" -> {
                    // 切换路由
                    flutterEngine.navigationChannel.pushRoute("/app")
                }

                "Back" -> {
                    onBackPressed()
                }

                else -> {

                }
            }
        }
    }

    override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
        super.cleanUpFlutterEngine(flutterEngine)
    }

    override fun provideFlutterEngine(context: Context): FlutterEngine? {
//        val app = this.applicationContext as Application
//        val flutterLoader = FlutterInjector.instance().flutterLoader()
//        if (!flutterLoader.initialized()){
//
//        }
//        val dartEntrypoint =
//            DartExecutor.DartEntrypoint(
//                flutterLoader.findAppBundlePath(), "towMain"
//            )
//        return FlutterEngineGroup(app).createAndRunEngine(this, dartEntrypoint)
//        return super.provideFlutterEngine(context)
        val fileName = intent.getStringExtra(EXTRA_DART_FILE) ?: ""
        val func = intent.getStringExtra(EXTRA_FUN_NAME) ?: ""
        val route = intent.getStringExtra(EXTRA_ROOT_ROUTE) ?: ""
        val pageRoute = intent.getStringExtra(EXTRA_PAGE_ROUTE) ?: ""
//        // 创建 engine 实例
//        val engine = FlutterEngine(application)
//        // flutter 引擎集合
//        val flutterEngineGroup = FlutterEngineGroup(application)
//        // 创建 挂载点 指定 函数名
//        engine.dartExecutor.executeDartEntrypoint(FlutterHelper.createPoint(fileName, func))
//        // 设置默认 路由名称
//        engine.navigationChannel.setInitialRoute(route)
//        if (pageRoute != "") {
//            engine.navigationChannel.pushRoute(pageRoute)
//        }
        // 加入缓存
//        FlutterEngineCache.getInstance().put(func, engine)

        Log.d(TAG, "provideFlutterEngine: ${FlutterEngineCache.getInstance()}")
        // 创建参数 引擎 启动参数配置
        val option: FlutterEngineGroup.Options = FlutterEngineGroup.Options(application)
        option.dartEntrypoint = FlutterHelper.createPoint(fileName, func)
        option.initialRoute = route
        val engine = FlutterEngineGroup(application).createAndRunEngine(option)
        if (pageRoute != "") {
            engine.navigationChannel.pushRoute(pageRoute)
        }
        Log.d(TAG, "provideFlutterEngine: ${engine.hashCode()}")
        // 缓存
        FlutterEngineCache.getInstance().put(func, engine)
        return engine
    }

    override fun onBackPressed() {
        super.onBackPressed()

    }

    override fun onDestroy() {
        super.onDestroy()

    }
}